package com.app.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.app.common.SystemConstant;
import com.app.conf.RedisService;
import com.app.model.UserModel;
import com.app.server.WxWebSocketServer;
import com.app.service.*;
import com.app.util.*;
import lombok.Data;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Data
@Controller
public class LoginController {

    @Autowired
    UserService userService;
    @Autowired
    LoginService loginService;
    @Autowired
    RedisService redisService;

    //登录
    @GetMapping("/doLogin")
    public Object doLogin(String userCode,String mobile, Integer type, String mobileCode,
                                  @RequestParam(required = false) String phoneInfo, String passWord) throws Exception {
        Object obj = userService.doLogin(mobile, type, mobileCode, phoneInfo, passWord,userCode);
        return obj;
    }

    @GetMapping("/getUserInfo")
    public Result<Object> getUserInfo(HttpServletRequest request, Integer type) {
        String token = request.getHeader("token");
        String mobile = TokenUtils.nothingness(token); 
        if (mobile == null) {
            return Result.error(405, "token不存在");
        }
        UserModel user = userService.getUserByMobile(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(userService.getUserInfo(type, user));
    }

    /**
     * 创建 login 数据
     *
     * @return 文件名
     */
    @PostMapping("create")
    public Result<Object> createUserLogin() throws Exception {
        return Result.success(loginService.createAndGetQrcodeFile());
    }


    /**
     * 通知WEB登录
     *
     */
    @PostMapping("getWxUserInfo")
    public Result<Object> getWxUserInfo(String code, String id) throws Exception {
        return Result.success(loginService.createAndGetQrcodeFile());
    }

    /**
     * 推送消息以扫码
     *
     */
    @GetMapping("doScanSuccess")
    public Result<Object> doScanSuccess(String scene) throws IOException {
        WxWebSocketServer.sendToUser("200",scene);
        return Result.success("扫码成功");
    }

    /**
     * 小程序确认登录后请求
     *
     */
    @GetMapping("doScanLogin")
    public Result<Object> doScanLogin(HttpServletRequest request,String scene) throws IOException {
        String token = request.getHeader("token");
        String mobile = TokenUtils.verify(token);
        JSONObject json = new JSONObject();
        json.put("userName",mobile);
        UserModel user = userService.getUserByMobile(mobile);
        if(StringTool.isNotEmpty(user.getPassWord()))
        {
            String passWord = AESTool.dealAESDecrypt(user.getPassWord(),"szjy");
            json.put("passWord",passWord);
        }else {
            json.put("passWord","");
        }
        WxWebSocketServer.sendToUser(json.toString(),scene);
        File file = new File(SystemConstant.DEFAULT_PDF_URL+"/wx-login/"+scene+".jpg");
        file.delete();
        return Result.success("登录成功");
    }

    /**
     * 小程序取消扫码登录通知
     *
     */
    @GetMapping("doScanCancel")
    public Result<Object> doScanCancel(String scene) throws IOException {
        WxWebSocketServer.sendToUser("300",scene);
        return Result.success("取消登录");
    }





}